package com.example.l.zcq1;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.view.LineChartView;

public class shujufenxi extends Fragment {
    public String[]spinnerData=new String[]{"数据分析1","数据分析2"};

    private Button button1;
    Spinner spinner1;
    LineChartView lineChartView;

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle){
        View view=layoutInflater.inflate(R.layout.sjfx,viewGroup,false);
        spinner1=(Spinner)view.findViewById(R.id.sjfx_spinner_1);
        button1=(Button)view.findViewById(R.id.sjfx_button_1);
        lineChartView=(LineChartView)view.findViewById(R.id.sjfx_lecho);
        spinner1.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,spinnerData));
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChart();
            }
        });
        return view;
    }

    public void showChart(){
        List<PointValue>pointValues=new ArrayList<>();
        pointValues.add(new PointValue(0,10));
        pointValues.add(new PointValue(1,20));
        pointValues.add(new PointValue(2,30));
        pointValues.add(new PointValue(3,40));
        pointValues.add(new PointValue(4,50));
        pointValues.add(new PointValue(5,60));
        pointValues.add(new PointValue(6,70));
        pointValues.add(new PointValue(7,80));


        Line line1=new Line(pointValues).setColor(Color.GRAY);
        List<Line>lines=new ArrayList<>();
        lines.add(line1);
        LineChartData data=new LineChartData();
        data.setLines(lines);

        int[]num=new int[]{0,5,10,20,25,30,35};

        List<AxisValue>Y=new ArrayList<>();
        for (int i=0;i<num.length;i++){
            Y.add(new AxisValue(i).setValue(num[i]));
        }

        String[] str2=new String[]{"1小时","2小时","3小时","4小时","5小时"};
        List<AxisValue>X=new ArrayList<>();
        for (int i=0;i<str2.length;i++){
            X.add(new AxisValue(i).setLabel(str2[i]));
        }


        Axis axisX=new Axis();
        Axis axisY=new Axis();

        axisX.setName("时间");
        axisY.setName("数值");

        data.setAxisXBottom(axisX);
        data.setAxisYLeft(axisY);

        axisX.setValues(X);
        axisY.setValues(Y);

        lineChartView.setLineChartData(data);
    }
}
